﻿#-------------------------------------------------------------------------------
# Name:        ElementsHelper
# Purpose:
#
# Author:      evob0413
#
# Created:     14.07.2014
# Copyright:   (c) evob0413 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from InformationGatheringConstants import *
from InformationGatheringConstants import ConfiguringParameters
from ElementsFinder import ElementsFinder
from ImageRecognizer import ImageRecognizer

class ElementsHelper(object):

    finder = ElementsFinder()
    recognizer = ImageRecognizer()
    parameters = ConfiguringParameters()

    # Точка входа
    def get_elements_map(self):
        elements_map = {}
        elements_map["players"] = self.get_players()
        elements_map["is_active"] = self.is_active()
        elements_map["hand"] = self.get_hand()
        elements_map["table_cards"] = self.get_table_cards()
        elements_map["last_message"] = self.get_last_message()
        elements_map["cash"] = self.get_cash()
        print elements_map
        return elements_map

    def get_players(self):
        # PLAYERS_COUNT игроков по умолчанию
        players = []
        for position in range(0, PLAYERS_COUNT):
            player = self.get_player(position)
            players.append(player)
        return players

    def get_player(self, position):
        player = {}
        player[KEY_NICKNAME] = self.get_nickname(position)
        player[KEY_BALANCE] = self.get_balance(position)
        player[KEY_BET] = self.get_bet(position)
        player[KEY_ACTION] = self.get_action(position)
        return player

    def get_nickname(self, position):
        left = self.parameters.parameters["PLAYER_NICKNAME_LEFT_" + str(position)]
        upper = self.parameters.parameters["PLAYER_NICKNAME_UPPER_" + str(position)]
        right = self.parameters.parameters["PLAYER_NICKNAME_RIGHT_" + str(position)]
        bottom = self.parameters.parameters["PLAYER_NICKNAME_BOTTOM_" + str(position)]
        color = self.parameters.parameters["COLOR_TEXT_NICKNAME"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

    def get_balance(self, position):
        left = self.parameters.parameters["PLAYER_BALANCE_LEFT_" + str(position)]
        upper = self.parameters.parameters["PLAYER_BALANCE_UPPER_" + str(position)]
        right = self.parameters.parameters["PLAYER_BALANCE_RIGHT_" + str(position)]
        bottom = self.parameters.parameters["PLAYER_BALANCE_BOTTOM_" + str(position)]
        color = self.parameters.parameters["COLOR_TEXT_BALANCE"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

    def get_bet(self, position):
        left = self.parameters.parameters["PLAYER_BET_LEFT_" + str(position)]
        upper = self.parameters.parameters["PLAYER_BET_UPPER_" + str(position)]
        right = self.parameters.parameters["PLAYER_BET_RIGHT_" + str(position)]
        bottom = self.parameters.parameters["PLAYER_BET_BOTTOM_" + str(position)]
        color = self.parameters.parameters["COLOR_TEXT_BET"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

    def get_action(self, position):
        left = self.parameters.parameters["PLAYER_ACTION_LEFT_" + str(position)]
        upper = self.parameters.parameters["PLAYER_ACTION_UPPER_" + str(position)]
        right = self.parameters.parameters["PLAYER_ACTION_RIGHT_" + str(position)]
        bottom = self.parameters.parameters["PLAYER_ACTION_BOTTOM_" + str(position)]
        color = self.parameters.parameters["COLOR_TEXT_ACTION"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

    def is_active(self):
        left = self.parameters.parameters["IS_ACTIVE_LEFT"]
        upper = self.parameters.parameters["IS_ACTIVE_UPPER"]
        right = self.parameters.parameters["IS_ACTIVE_RIGHT"]
        bottom = self.parameters.parameters["IS_ACTIVE_BOTTOM"]
        color = self.parameters.parameters["COLOR_IS_ACTIVE"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

    def get_hand(self):
        hand = {}
        hand[KEY_CARD_HAND_0] = self.get_card(POSITION_CARD_HAND_0)
        hand[KEY_CARD_HAND_1] = self.get_card(POSITION_CARD_HAND_1)
        return hand

    def get_card(self, position):
        return "mock_card"

    def get_table_cards(self):
        table_cards = {}
        table_cards[KEY_CARD_TABLE_0] = self.get_card(POSITION_CARD_TABLE_0)
        table_cards[KEY_CARD_TABLE_1] = self.get_card(POSITION_CARD_TABLE_1)
        table_cards[KEY_CARD_TABLE_2] = self.get_card(POSITION_CARD_TABLE_2)
        table_cards[KEY_CARD_TABLE_3] = self.get_card(POSITION_CARD_TABLE_3)
        table_cards[KEY_CARD_TABLE_4] = self.get_card(POSITION_CARD_TABLE_4)
        return table_cards

    def get_last_message(self):
        left = self.parameters.parameters["LAST_MESSAGE_LEFT"]
        upper = self.parameters.parameters["LAST_MESSAGE_UPPER"]
        right = self.parameters.parameters["LAST_MESSAGE_RIGHT"]
        bottom = self.parameters.parameters["LAST_MESSAGE_BOTTOM"]
        color = self.parameters.parameters["COLOR_TEXT_MESSAGE"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

    def get_cash(self):
        left = self.parameters.parameters["TOTAL_LEFT"]
        upper = self.parameters.parameters["TOTAL_UPPER"]
        right = self.parameters.parameters["TOTAL_RIGHT"]
        bottom = self.parameters.parameters["TOTAL_BOTTOM"]
        color = self.parameters.parameters["COLOR_TEXT_CASH"]
        element = self.finder.findElement(left, upper, right, bottom)
        return self.recognizer.recognize_text(color, element)

def main():
    helper = ElementsHelper()
    helper.get_elements_map()

if __name__ == '__main__':
    main()
